<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "insurance_price".
 *
 * @property integer $id
 * @property string $project_code
 * @property string $plan_code
 * @property string $price_name_time
 * @property string $price_name_age
 * @property integer $price_day
 * @property integer $price_year
 * @property integer $start_age
 * @property integer $end_age
 * @property string $price_amount
 * @property string $price_currency
 * @property string $create_time
 * @property string $status
 */
class InsurancePrice extends \yii\db\ActiveRecord
{
    const STATUS_SUCCESS = 'ok';
    const STATUS_FALSE   = 'no';

    /**
     * 根据所选的价格编号，获取相关产品信息
     */
    public static function getInsuranceById($price_id){
        return static::find()->from(self::tableName() . ' as a')
            ->leftJoin(InsuranceProject::tableName() . ' as b', 'a.project_code = b.project_code')
            ->leftJoin(InsurancePlan::tableName() . ' as c', 'a.plan_code = c.plan_code')
            ->where(['a.id' => $price_id])
            ->select(['a.project_code','a.plan_code','a.price_name_time','a.price_day','a.price_amount','a.price_currency','b.project_name','b.provider_id','c.plan_name','c.plan_fullname'])
            ->orderBy('a.id asc')->asArray()->limit(1)->one();
    }

    

    /**
     * 获取某一个产品计划的价格分类
     */
    public static function getInsurancePriceByPlan($plan_code){
        return static::find()
            ->where(['plan_code' => $plan_code])
            ->andWhere(['status' => static::STATUS_SUCCESS])
            ->orderBy('id asc')->asArray()->all();
    }


    /**
     * 获取某一个产品的所有计划以及产品价格
     */
    public static function getInsurancePriceByCode($project_code){

        return static::find()->from(self::tableName() . ' as a')
            ->leftJoin(InsurancePlan::tableName() . ' as b', 'a.plan_code = b.plan_code')
            ->where(['a.project_code' => $project_code])
            ->andWhere(['a.status' => static::STATUS_SUCCESS])
            ->select(['a.id','a.project_code','a.plan_code','a.price_name_time','a.price_day','a.price_amount','price_currency','b.plan_order','b.plan_name','b.plan_fullname'])
            ->orderBy('a.id asc')->asArray()->all();
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'insurance_price';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['price_day', 'price_year', 'start_age', 'end_age'], 'integer'],
            [['price_amount'], 'number'],
            [['create_time'], 'safe'],
            [['project_code', 'plan_code', 'price_name_time', 'price_name_age'], 'string', 'max' => 32],
            [['price_currency'], 'string', 'max' => 3],
            [['status'], 'string', 'max' => 16],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_code' => 'Project Code',
            'plan_code' => 'Plan Code',
            'price_name_time' => 'Price Name Time',
            'price_name_age' => 'Price Name Age',
            'price_day' => 'Price Day',
            'price_year' => 'Price Year',
            'start_age' => 'Start Age',
            'end_age' => 'End Age',
            'price_amount' => 'Price Amount',
            'price_currency' => 'Price Currency',
            'create_time' => 'Create Time',
            'status' => 'Status',
        ];
    }
}
